package cn.lls.controller.common;

import cn.lls.base.common.LlsConstant;
import cn.lls.base.result.R;
import cn.lls.entity.common.RegionDictEntity;
import cn.lls.service.IRegionDictService;
import cn.lls.service.impl.OSSPolicyService;
import cn.lls.vo.common.RegionCascaderVO;
import cn.lls.vo.common.RegionDictVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CommonController
 * @Author 划水艺术家
 * @Date 2022/11/16 18:15
 * @Version 1.0
 **/

@RestController
@RequestMapping("/common")
@Api(tags = "六六顺-通用操作-控制器")
public class CommonController {

    @Autowired
    private OSSPolicyService ossPolicyService;

    @Autowired
    private IRegionDictService regionDictService;

    /**
     * 获取OSS直传签名
     */
    @GetMapping("/oss/policy/{model}")
    @ApiOperation(value = "获取OSS直传签名", notes = "获取OSS直传签名, 有效时间30分钟", response = R.class, httpMethod = "GET")
    public R getOSSPolicy(@PathVariable Integer model){
        return ossPolicyService.policy(model);
    }



    /*
       获取地区 options
    */
    @GetMapping(value = "/region/option", produces = "application/json; charset=UTF-8")
    public R getRegion(){
        return R.success(getRegionCascaderVOs(LlsConstant.CHINA_REGION_CODE));
    }

    /*
     获取地区
     */
    @GetMapping("/region")
    @ApiOperation(value = "获取地区信息", notes = "获取地区信息", response = R.class, httpMethod = "GET")
    public R getProvince(@RequestParam(required = false) @ApiParam("待查询地区的regionCode") String parentCode){
        return R.success(getRegionDictVOs(
                StringUtils.isBlank(parentCode) ? LlsConstant.CHINA_REGION_CODE : parentCode
        ));
    }

    /*
    根据 parentCode 获取 相应地区信息
     */
    private List<RegionDictVO> getRegionDictVOs(String parentCode){
        LambdaQueryWrapper<RegionDictEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionDictEntity::getParentCode, parentCode);
        return regionDictService.list(queryWrapper).stream().map(regionDictEntity -> {
            RegionDictVO regionDictVO = new RegionDictVO();
            BeanUtils.copyProperties(regionDictEntity, regionDictVO);
            return regionDictVO;
        }).collect(Collectors.toList());
    }

    /*
    根据 parentCode 获取 相应地区信息
     */
    private List<RegionCascaderVO> getRegionCascaderVOs(String parentCode){
        LambdaQueryWrapper<RegionDictEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionDictEntity::getParentCode, parentCode);
        List<RegionCascaderVO> collect = regionDictService.list(queryWrapper).stream().map(regionDictEntity -> {
            RegionCascaderVO regionCascaderVO = new RegionCascaderVO();
            regionCascaderVO.setValue(regionDictEntity.getId());
            regionCascaderVO.setLabel(regionDictEntity.getRegionName());
            regionCascaderVO.setCode(regionDictEntity.getRegionCode());
            if (regionDictEntity.getLevelType() != 3)
                regionCascaderVO.setChildren(getRegionCascaderVOs(regionDictEntity.getRegionCode()));
            return regionCascaderVO;
        }).collect(Collectors.toList());

        return collect;
    }
}
